package locacao.integracao.dao.pstPostgreSQL;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;

import locacao.integracao.dao.ItemServicoDAO;
import locacao.negocio.to.ItemServicoTO;

public class ItemServicoPgsqlDAO implements ItemServicoDAO {

    private ArrayList<ItemServicoTO> itemServicos;

    public ItemServicoPgsqlDAO(){
	   setItemServicos(new ArrayList<ItemServicoTO>());
    }


    public void addOrUpdateItemServico(ItemServicoTO itemServico) {
    	Connection conn = null;
    	CallableStatement spManterItemServico = null;

    	try {        	
          String sql = "{call locacao.manter_itemServico(?,?,?,?)}";
          conn = ConnectionRentACar.getInstance().getConnection();
          
          spManterItemServico = conn.prepareCall(sql);
          
          spManterItemServico.setInt(1, itemServico.getIdLocacao());
          spManterItemServico.setInt(2, itemServico.getIdServico());
          spManterItemServico.setInt(3, itemServico.getQuantidadeServico());
          spManterItemServico.setInt(4, (int) itemServico.getValorUnitario());
          
          spManterItemServico.execute();
        } catch (Exception e) {
          System.out.println(e.getMessage());
        } finally {
          try {
            if (spManterItemServico != null)
            	spManterItemServico.close();

          } catch (SQLException e) {
            System.out.println(e.getMessage());
          }
        }
    }

    /**
     *

    /**
     * @param itemServico
     */
    public void excludeItemServico( ItemServicoTO itemServico )  {
    	Connection conn = null;
    	CallableStatement spDeleteItemServico = null;

    	try {        	
          String sql = "{call locacao.delete_itemServico(?,?)}";
          conn = ConnectionRentACar.getInstance().getConnection();
          
          spDeleteItemServico = conn.prepareCall(sql);
          
          spDeleteItemServico.setInt(1, itemServico.getIdLocacao());   
          spDeleteItemServico.setInt(2, itemServico.getIdServico());
          
          spDeleteItemServico.execute();
        } catch (Exception e) {
          System.out.println(e.getMessage());
        } finally {
          try {
            if (spDeleteItemServico != null)
            	spDeleteItemServico.close();

          } catch (SQLException e) {
            System.out.println(e.getMessage());
          }
        }
    }


  
    /**
     * @return
     */
    public String[] findItemServicoById(Integer idLocacao, Integer idServico){
    	 Connection conn = null;
    	 Statement stmt = null;
    	 ResultSet rSet = null;
    	 String[] info = null;

    	 String sql = "SELECT  idlocacao, idservico, qtdservico, vlrunit ";
    	 sql += " FROM locacao.itemServico WHERE idlocacao =  " + idLocacao + " and idservico = " + idServico;

    	 try {
    		 conn = ConnectionRentACar.getInstance().getConnection();
    		 stmt = conn.createStatement();
    		 rSet = stmt.executeQuery(sql);

    		 if (rSet != null) {    			 
    			 while (rSet.next()) {
    				 
    				info = new String[itemServicos.size()];
    	    		   info[0] = rSet.getInt("idlocacao") 
    	       		   + " - " + rSet.getInt("idservico") 
    	       		   + " - " + rSet.getInt("qtdservico")
    	       		   + " - " + rSet.getFloat("vlrunit");   		   
    			 }    			 
    		 }
			 return info;
			 
    	 } catch (Exception e) {
    		 System.out.println(e.getMessage());
    		 return null;
    	 } finally {
    		 try {
    			 if (rSet != null)
    				 rSet.close();
    			 if (stmt != null)
    				 stmt.close();
    		 } catch (SQLException e) {
    			 System.out.println(e.getMessage());
    		 }
    	 }
    }

    
    /**
     * @return
     */
    public String[] getInfoItemServicos()
    {
        ItemServicoTO itemServico;
        itemServicos = getItemServicos();
        String[] info = new String[itemServicos.size()];
        Iterator iteratorItemServicos;
        iteratorItemServicos = itemServicos.iterator();
        int i=0;
        while ( iteratorItemServicos.hasNext() )
        {
            itemServico = (ItemServicoTO) iteratorItemServicos.next();
            info[i] = itemServico.getIdLocacao() 
            		+ " - " + itemServico.getIdServico()   
            		+ " - " + itemServico.getQuantidadeServico()
            		+ " - " + itemServico.getValorUnitario();
            i++;
        }
        return info;
    }


    /**
     * @return
     */
    public ArrayList<ItemServicoTO> getItemServicos() {
    	 Connection conn = null;
    	 Statement stmt = null;
    	 ResultSet rSet = null;
    	 ItemServicoTO itemServico = null;

    	 String sql = "SELECT  idlocacao, idservico, qtdservico, vlrunit ";
    	 sql += " FROM locacao.itemServico ORDER BY idlocacao";

    	 try {
    		 conn = ConnectionRentACar.getInstance().getConnection();
    		 stmt = conn.createStatement();
    		 rSet = stmt.executeQuery(sql);

    		 if (rSet != null) {
    			 itemServicos = new ArrayList();
    			 while (rSet.next()) {
    				 
    				 itemServico = new ItemServicoTO();
    				 
    				 itemServico.setIdLocacao(rSet.getInt("idlocacao"));
    				 itemServico.setIdServico(rSet.getInt("idservico"));
    				 itemServico.setQuantidadeServico(rSet.getInt("qtdservico"));
    				 itemServico.setValorUnitario(rSet.getFloat("vlrunit"));
    				 
    				 itemServicos.add(itemServico);
    			 }
    		 }
    	 } catch (Exception e) {
    		 System.out.println(e.getMessage());
    	 } finally {
    		 try {
    			 if (rSet != null)
    				 rSet.close();
    			 if (stmt != null)
    				 stmt.close();
    		 } catch (SQLException e) {
    			 System.out.println(e.getMessage());
    		 }
    	 }
    	 return itemServicos;
    }

    private void setItemServicos(ArrayList<ItemServicoTO> itemServicos) {
    	this.itemServicos = itemServicos;
    }

    
}
